package com.mfsoft.utilities;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import com.mfsoft.model.Account;
import com.mfsoft.model.UserActions;

public class Utility {
	public static String prefname = "my_data";
	public static String md5(String s) 
	{
	    MessageDigest digest;
	    try 
	    {
	        digest = MessageDigest.getInstance("MD5");
	        digest.update(s.getBytes(),0,s.length());
	        String hash = new BigInteger(1, digest.digest()).toString(16);
	        return hash;
	    } 
	    catch (NoSuchAlgorithmException e) 
	    {
	        e.printStackTrace();
	    }
	    return "";
	}
	public static void setAccount(Context context, Account account){
		SharedPreferences preferences = ((Activity)context).getSharedPreferences(prefname,((Activity)context).MODE_PRIVATE);	
		preferences.edit().putString(UserActions.GMAIL,account.getGmail()).commit();
		preferences.edit().putString(UserActions.USERID, account.getUserId()).commit();
		preferences.edit().putString(UserActions.BIRTHDAY, account.getBirthday()).commit();
		preferences.edit().putString(UserActions.GENDER, account.getGender()).commit();
		preferences.edit().putString(UserActions.USERNAME, account.getUserName()).commit();
		preferences.edit().putString(UserActions.TYPE, account.getType()).commit();
		preferences.edit().putString(UserActions.NICKNAME, account.getNickname()).commit();
		
	}
	public static void read(Context context, Account account){
		SharedPreferences preferences = ((Activity)context).getSharedPreferences(prefname,((Activity)context).MODE_WORLD_READABLE);	
		preferences.edit().putString(UserActions.GMAIL,account.getGmail()).commit();
		preferences.edit().putString(UserActions.USERID, account.getUserId()).commit();
		preferences.edit().putString(UserActions.BIRTHDAY, account.getBirthday()).commit();
		preferences.edit().putString(UserActions.GENDER, account.getGender()).commit();
		preferences.edit().putString(UserActions.USERNAME, account.getUserName()).commit();
		preferences.edit().putString(UserActions.TYPE, account.getType()).commit();
		preferences.edit().putString(UserActions.NICKNAME, account.getNickname()).commit();
		
	}
	public static void write(Context context, Account account){
		SharedPreferences preferences = ((Activity)context).getSharedPreferences(prefname,((Activity)context).MODE_WORLD_WRITEABLE);	
		preferences.edit().putString(UserActions.GMAIL,account.getGmail()).commit();
		preferences.edit().putString(UserActions.USERID, account.getUserId()).commit();
		preferences.edit().putString(UserActions.BIRTHDAY, account.getBirthday()).commit();
		preferences.edit().putString(UserActions.GENDER, account.getGender()).commit();
		preferences.edit().putString(UserActions.USERNAME, account.getUserName()).commit();
		preferences.edit().putString(UserActions.TYPE, account.getType()).commit();
		preferences.edit().putString(UserActions.NICKNAME, account.getNickname()).commit();
		
	}
	
}
